﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyPhongMach.Context;
using QuanLyPhongMach.Platform;
using QuanLyPhongMach.Model;

namespace QuanLyPhongMach.View
{
    /// <summary>
    /// Interaction logic for WCachDung.xaml
    /// </summary>
    public partial class WCachDung : Page
    {
        #region Variable
        private CCachDungContext cachdungContext;

        private bool isThem;
        private bool isCapNhatXoa;
        #endregion

        #region Constructor
        public WCachDung()
        {
            InitializeComponent();
            InitializeFeature();
            InitializeFlag();
        }

        private void InitializeFeature()
        {
            cachdungContext = new CCachDungContext(CConstant.CONNECTION);
            TrangThaiMacDinh();
            try
            {
                KhoiTaoView();
                CapNhatBang();
            }
            catch { }
        }

        private void InitializeFlag()
        {
            isThem = false;
            isCapNhatXoa = false;
        }
        #endregion

        #region Function
        public void LamMoi()
        {
            cachdungContext.LamMoi();
        }

        private void CapNhatBang()
        {
            viewCachDung.ItemsSource = cachdungContext.LayDanhSachTatCa();
        }

        private void KhoiTaoView()
        {
            GridView grdView = new GridView();
            grdView.AllowsColumnReorder = true;
            grdView.ColumnHeaderToolTip = "DONVI";

            GridViewColumn IDColumn = new GridViewColumn();
            IDColumn.DisplayMemberBinding = new Binding("MaCachDung");
            IDColumn.Header = "Mã Đơn Vị";
            IDColumn.Width = 120;
            grdView.Columns.Add(IDColumn);

            GridViewColumn nameColumn = new GridViewColumn();
            nameColumn.DisplayMemberBinding = new Binding("TenCachDung");
            nameColumn.Header = "Tên Đơn Vị";
            nameColumn.Width = 120;
            grdView.Columns.Add(nameColumn);

            viewCachDung.View = grdView;
        }

        private void ChonRecord()
        {
            try
            {
                TrangThaiCapNhatXoa();
                if (viewCachDung.SelectedItem is CCachDung)
                {
                    txtMaCachDung.Text = (viewCachDung.SelectedItem as CCachDung).MaCachDung.ToString();
                    txtTenCachDung.Text = (viewCachDung.SelectedItem as CCachDung).TenCachDung;
                }
            }
            catch { }
        }

        private void XoaRecord()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    //Xóa loại bênh có mã số đang hiện hành
                    cachdungContext.Xoa(int.Parse(txtMaCachDung.Text));
                    MessageBox.Show("Xóa Thành Công!", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                    CapNhatBang();
                }
            }
            catch
            {
                MessageBox.Show("Xóa Thất Bại!", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            TrangThaiMacDinh();
        }

        private void ThemRecord()
        {
            TrangThaiThem();
            txtMaCachDung.Text = cachdungContext.LayMaSoMoi().ToString();
        }

        private void LuuRecord()
        {
            if (isThem)
            {
                LuuMoiRecord();
                ThemRecord();
            }
            else
                if (isCapNhatXoa)
                {
                    CapNhatRecord();
                }
            CapNhatBang();
        }

        #region Tool Of LuuRecord
        private void LuuMoiRecord()
        {
            try
            {
                if (txtTenCachDung.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập Tên đơn vị");
                }
                else
                {
                    var donvi = new CCachDung(txtTenCachDung.Text);
                    cachdungContext.Them(donvi);
                }
            }
            catch { }
        }

        private void CapNhatRecord()
        {
            try
            {
                //Sửa
                var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    var donvi = new CCachDung { TenCachDung = txtTenCachDung.Text, MaCachDung = int.Parse(txtMaCachDung.Text) };
                    cachdungContext.CapNhat(donvi);
                    MessageBox.Show("Sửa Thành Công!", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch
            {
                MessageBox.Show("Sửa Thất Bại!", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        #endregion

        private void TrangThaiThem()
        {
            txtTenCachDung.Clear();
            txtTenCachDung.IsEnabled = true;
            txtTenCachDung.Focus();

            isThem = true;
            isCapNhatXoa = false;

            btnLuu.IsEnabled = true;
            btnXoa.IsEnabled = false;
        }

        private void TrangThaiCapNhatXoa()
        {
            isThem = false;

            isCapNhatXoa = true;
            btnXoa.IsEnabled = true;

            btnLuu.IsEnabled = true;

            txtTenCachDung.Clear();
            txtTenCachDung.IsEnabled = true;
            txtTenCachDung.Focus();
        }

        private void TrangThaiMacDinh()
        {
            isThem = false;
            isCapNhatXoa = false;

            btnXoa.IsEnabled = false;
            btnLuu.IsEnabled = false;

            txtTenCachDung.Clear();
            txtTenCachDung.IsEnabled = false;

            txtMaCachDung.Clear();
        }
        #endregion

        #region Event
        private void viewCachDung_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonRecord();
        }


        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaRecord();
        }

        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            ThemRecord();
        }

        private void btnLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuRecord();
        }

        private void txtTenCachDung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuRecord();
            }
        }
        #endregion
    }
}
